|
|
|
|
|
|
|
Creating a Connection String |
|
|
|
|
|
|
|
|
Notice that the ConnectionString property of the Connection object is set by calling a function. This function, ConnectString(), returns a valid ADO connection string. There are many forms this connection string can take. Here, its broken out into a function so that each form can be explained: |
|
|
|
 |
|
|
|
|
Public Function ConnectString() As String
' SQL Server using an ODBC Data Source
'ConnectString = "DSN=EmployeeSQLServer;" & _
' "UID=sa;PWD=;" & _
' "DATABASE=Employees"
' SQL Server using OLE DB Provider
'ConnectString = "Provider = sqloledb;" & _
' "Data Source = (local);" & _
' "Initial Catalog = Employees;" & _
' "User Id = sa;" & _
' "Password = ; "
' Jet MDB
ConnectString = "Provider=Microsoft.Jet.OLEDB.3.51" & _
";Data Source=" & App.Path & "\..\Employees.mdb"
End Function |
|
|
|
|
|
|
|
|
In this code, you can see three different versions of a connection string. The first one is used to connect to an ODBC data source. The DSN is the name of the data source you created in the ODBC Administrator. To use this version of the connection string, you need to create a DSN called EmployeeSQLServer. This needs to point to a SQL Server with a database on that server called Employees. |
|
|
|
|
|
|
|
|
The second connection string also points to the same SQL Server, but it uses the new OLE DB provider string syntax. The provider for SQL Server is called sqloledb. The data source is the name of the NT server thats used for SQL Server, and the initial catalog is the name of the database within SQL Server. |
|
|
|
|
|
|
|
|
The third version of the connection string is used to connect to a Jet MDB file. All you need is the provider value Microsoft.Jet.OLEDB.3.51 and then the data source needs to list the full path and filename of the MDB file you want to connect to. |
|
|
|
|
|